package gui.admin;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.swtdesigner.SWTResourceManager;

public class AdminWelcomePanelImpl extends AbstractAdminPanelImpl implements AdminWelcomePanel {

	private Label welcomeLbl;
	private Label instLbl;
	private Label usernameLbl;
	private Text username;
	private Label passwordLbl;
	private Text password;
	private Button loginBtn;
	
	private List<User> users = new ArrayList<User>();
	private Label label;
	private Label invalid;
	
	public AdminWelcomePanelImpl(Display display, Shell shell) {
		super(display, shell);
		
		users.add(new User("awill", "awill"));
		users.add(new User("ehill", "ehill"));
		users.add(new User("bdixon", "bdixon"));
		users.add(new User("adineros", "adineros"));
		users.add(new User("thristov", "thristov"));
		users.add(new User("jmarr", "jmarr"));
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	public void open() {
		// Remove at compile time
//		Shell shell = new Shell();
		// **********************
		
		shell.setSize(800, 600);
		shell.setText("Welcome");
		
		fileNewItem.setEnabled(false);
		fileDriversItem.setEnabled(false);
		
		welcomeLbl = new Label(shell, SWT.CENTER);
		welcomeLbl.setBounds(10, 10, 763, 97);
		welcomeLbl.setText("Welcome to the Defensive Driver's Administrator Interface");
		welcomeLbl.setFont(new Font(display, "Arial", 30, SWT.ITALIC|SWT.BOLD));
		
		instLbl = new Label(shell, SWT.CENTER);
		instLbl.setBounds(110, 373, 547, 37);
		instLbl.setText("Please enter your username and password.");
		instLbl.setFont(new Font(display, "Arial", 18, SWT.BOLD));
		
		usernameLbl = new Label(shell, SWT.NONE);
		usernameLbl.setBounds(120, 416, 109, 24);
		usernameLbl.setText("Username:");
		usernameLbl.setFont(new Font(display, "Arial", 16, SWT.BOLD));
		
		username = new Text(shell, SWT.BORDER);
		username.setBounds(235, 416, 395, 24);
		username.setFont(new Font(display, "Arial", 16, SWT.NONE));
		
		passwordLbl = new Label(shell, SWT.NONE);
		passwordLbl.setBounds(120, 446, 109, 24);
		passwordLbl.setText("Password:");
		passwordLbl.setFont(new Font(display, "Arial", 16, SWT.BOLD));
		
		password = new Text(shell, SWT.BORDER);
		password.setBounds(235, 446, 395, 24);
		password.setFont(new Font(display, "Arial", 16, SWT.NONE));
//		password.setEchoChar('*');
		
		loginBtn = new Button(shell, SWT.NONE);
		loginBtn.setBounds(526, 476, 104, 37);
		loginBtn.setText("Login");
		loginBtn.setFont(new Font(display, "Arial", 16, SWT.BOLD));
		loginBtn.addListener(SWT.Selection, new Listener() {
			@Override
			public void handleEvent(Event event) {
				if(correctUser()) {
					AdminDriverDisplayPanel window = new AdminDriverDisplayPanelImpl(display, shell);
					dispose();
					window.open();
				} else {
					invalid.setVisible(true);
				}
			}
		});
		
		
		label = new Label(shell, SWT.NONE);
		label.setImage(SWTResourceManager.getImage(AdminWelcomePanelImpl.class, "/images/DefensiveDriverLogo.gif"));
		label.setBounds(279, 113, 200, 236);
		
		invalid = new Label(shell, SWT.RIGHT);
		invalid.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		invalid.setBounds(120, 482, 400, 24);
		invalid.setText("Invalid Username or Password");
		invalid.setFont(new Font(display, "Arial", 16, SWT.NONE));
		invalid.setVisible(false);

		shell.open();
		shell.layout();
	}
	@Override
	public String getUsername() {
		return username.getText();
	}
	@Override
	public String getPassword() {
		return password.getText();
	}
	
	private class User {
		String username;
		String password;
		
		public User(String username, String password) {
			this.username = username;
			this.password = password;
		}
		
		public String getUsername() {
			return username;
		}
		
		public String getPassword() {
			return password;
		}
		
		public boolean equals(User toCompare) {
			return username.equals(toCompare.getUsername()) && password.equals(toCompare.getPassword());
		}
	}
	
	private boolean correctUser() {
		User toFind = new User(username.getText(), password.getText());
		
		for(User compare : users) {
			if(compare.equals(toFind)) {
				return true;
			}
		}
		
		return false;
	}
}
